import Vue from 'vue'
import VueRouter from 'vue-router';
import store from '../store';
import kkui from 'kkui/kkui';
import Index from 'views/Index';
import server from '../server';
import Login from 'views/Login';
import Page404 from 'views/error/Page404';


Vue.use(VueRouter);


const routes = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    name: 'index',
    meta: {
      title: '首页'
    },
    component: Index
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '用户登录'
    },
    component: Login
  },
  {
    path: '/test',
    name: 'test',
    meta: {
      title: 'test'
    },
    component: ()=>import('views/error/Test')
  },
  {
    path: '/video',
    name: 'video',
    meta: {
      title: 'video'
    },
    component: ()=>import('views/Video.vue')
  },
  {
    path: '/camera',
    name: 'camera',
    meta: {
      title: 'camera'
    },
    component: ()=>import('views/Camera.vue')
  },
  {
    path: '*',
    name: '404',
    meta: {
      title: '404'
    },
    component: Page404
  }
];

WebCfg.menu.forEach(item => {
  // console.log(item);
  const path = item.path;
  const _cfg = {
    path: '/' + path,
    name: item.title,
    meta: {
      title: item.title
    },
    component: () => import('views/' + item.path + '/Index'),
  };

  if (item.children) {
    _cfg.children = [];
    item.children.forEach(itm => {
      const path2 = itm.path;
      const compName = path2[0].toUpperCase() + path2.substr(1);
      _cfg.children.push({
        path: path2,
        name: item.path + '_' + itm.path,
        meta: {
          title: itm.title
        },
        component: () => import(`views/${path}/${compName}.vue`),
      })
    })
  }
  routes.push(_cfg);
});

const index=routes.findIndex(item=>item.path==='/check');
routes[index].children.forEach(item=>{
  if(item.path==='data'){
    item.children=[];
    item.redirect='/check/data/table';
    ['table','event','road','camera','report'].forEach(itm=>{
      item.children.push({
        path: itm,
        name: item.path+'_'+itm,
        meta: {
          title: item.meta.title
        },
        component: () => import(`views/check/data/${itm}.vue`),
      })
    })

  }
});



const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  const renewPageInfo = () => {
    kkui.renewTitle(to.meta['title'] + ' - ' + WebCfg.title);
    store.commit('renewPageInfo', {
      path: to.path || '',
      name: to.name || '',
      title: to.matched[0]['meta']['title'] + (to.matched.length === 2 ? ' - ' + to.matched[1]['meta']['title'] : '')
    });
  };

  const userInfo = store.state.userInfo,
    user = kkui.cache.get('user');
  renewPageInfo();

  if (userInfo && userInfo.id) {
    const identityId = userInfo.identityId;
    if (identityId !== '5' && identityId !== '2' && to.name.indexOf('system_') === 0){
      next('/404');
    }else{
      next();
    }
  } else {
    if (user && user['id']) {
      const identityId = user.identityId;
      server('user.checkToken', {
        id: user.id,
        token: user.token
      }).then(info => {
        kkui.cache.set('user', info);
        store.commit('renewUserInfo', info);
        if (identityId !== '5' && identityId !== '2' && to.name.indexOf('system_') === 0){
          next('/404');
        }else{
          next();
        }
      })

    } else {
      to.path !== '/login' ? next('/login') : next();
    }
  }
});

export default router
